package com.wad.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wad.dto.system.SysRoleDto;
import com.wad.entity.system.SysRole;
import com.wad.manager.mapper.SysRoleMapper;
import com.wad.manager.mapper.SysUserRoleMapper;
import com.wad.manager.service.SysRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wad
 * @date 2023/12/01 10:57
 * @project e-commerce
 **/
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public PageInfo<SysRole> findByPage(SysRoleDto sysRoleDto, Integer current, Integer limit) {
        //设置分页参数
        PageHelper.startPage(current,limit);
        //根据条件查询所有数据
        List<SysRole> sysRoles=sysRoleMapper.findByPage(sysRoleDto);
        //封装PageInfo对象
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoles);
        return pageInfo;
    }


    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.save(sysRole);
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.update(sysRole);
    }

    @Override
    public void deleteById(Long roleId) {
        sysRoleMapper.deleteById(roleId);
    }


    //查询所有角色
    @Override
    public Map<String, Object> findAll(Long userId) {
        Map<String,Object> map=new HashMap<>();
        //查询所有角色
        List<SysRole> sysRoleList=sysRoleMapper.findAll();
        map.put("allRoleList",sysRoleList);
        //根据用户id查询当前用户分配过的角色
        List<Long> roleIds=sysUserRoleMapper.selectRoleIdsByUserId(userId);
        map.put("sysUserRoles",roleIds);
        return map;
    }
}
